package com.ruoyi.web.controller.ycdj;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.system.domain.TScoretype;
import com.ruoyi.system.domain.TWorkscore;
import com.ruoyi.system.service.TScoretypeService;
import com.ruoyi.system.service.TWorkscoreService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("score")
@Api(tags = "积分")
public class ScoreController extends BaseController {

    @Autowired
    public TScoretypeService tScoretypeService;

    @Autowired
    public TWorkscoreService tWorkscoreService;


    @ApiOperation("积分类型列表")
    @GetMapping("scoretypelist")
    public AjaxResult scoretypelist(@RequestParam(value="pageNum",defaultValue = "")Integer pageNum,
                                    @RequestParam(value="pageSize",defaultValue = "")Integer pageSize
                                    ){
        QueryWrapper<TScoretype>tScoretypeQueryWrapper=new QueryWrapper<>();
        tScoretypeQueryWrapper.eq("status",1);
        if(pageNum!=null){
            PageHelper.startPage(pageNum,pageSize);
            List<TScoretype>tScoretypeList=tScoretypeService.list(tScoretypeQueryWrapper);
            PageInfo<TScoretype>pageInfo=new PageInfo<>(tScoretypeList);
            return AjaxResult.success(pageInfo);
        }else{
            List<TScoretype>tScoretypeList=tScoretypeService.list(tScoretypeQueryWrapper);
            return AjaxResult.success(tScoretypeList);
        }

    }

    @ApiOperation("新增积分类型")
    @PostMapping("addscoretype")
    public AjaxResult addscoretype(@RequestBody TScoretype tScoretype){
        return toAjax(tScoretypeService.save(tScoretype));
    }

    @ApiOperation("编辑积分类型")
    @PostMapping("editscoretype")
    public AjaxResult editscoretype(@RequestBody TScoretype tScoretype){
        return toAjax(tScoretypeService.saveOrUpdate(tScoretype));
    }

    @ApiOperation("删除积分类型")
    @GetMapping("deletescoretype")
    public AjaxResult deletescoretype(Integer id){
        TScoretype tScoretype=tScoretypeService.getById(id);
        tScoretype.setStatus(0);
        return toAjax(tScoretypeService.saveOrUpdate(tScoretype));
    }

    //积分

    @ApiOperation("积分列表")
    @GetMapping("getlist")
    public AjaxResult getlist(@RequestParam(value="pid",defaultValue = "")Integer pid){
        QueryWrapper<TScoretype>tScoretypeQueryWrapper=new QueryWrapper<>();
        if(pid!=null){
            tScoretypeQueryWrapper.eq("pid",pid);
        }
        tScoretypeQueryWrapper.eq("status",1);
        List<TScoretype>tScoretypeList=tScoretypeService.list(tScoretypeQueryWrapper);
        if(!tScoretypeList.isEmpty()){
            for(TScoretype tScoretype:tScoretypeList){
                tScoretype.setLabel_id("p_"+tScoretype.getId());
                tScoretype.setChildlist(getchildlist(tScoretype.getId(),tScoretype.getName()));
            }
        }
        return AjaxResult.success(tScoretypeList);
    }

    //获取积分类型下的积分列表
    public List<TWorkscore> getchildlist(Integer pid,String pname){
        QueryWrapper<TWorkscore>tWorkscoreQueryWrapper=new QueryWrapper<>();
        tWorkscoreQueryWrapper.eq("typeid",pid);
        tWorkscoreQueryWrapper.eq("status",1);

        List<TWorkscore>tWorkscoreList=tWorkscoreService.list(tWorkscoreQueryWrapper);
        if(!tWorkscoreList.isEmpty()){
            for(TWorkscore tWorkscore:tWorkscoreList){
                tWorkscore.setLabel_id("c_"+tWorkscore.getId());
                tWorkscore.setTypename(pname);
            }
        }
        return tWorkscoreList;
    }

    //新增积分
    @ApiOperation("新增积分")
    @PostMapping("add")
    public AjaxResult add(@RequestBody TWorkscore tWorkscore){
        return toAjax(tWorkscoreService.save(tWorkscore));
    }

    @ApiOperation("编辑积分")
    @PostMapping("edit")
    public AjaxResult edit(@RequestBody TWorkscore tWorkscore){
        return toAjax(tWorkscoreService.saveOrUpdate(tWorkscore));
    }

    @ApiOperation("删除积分")
    @GetMapping("delete")
    public AjaxResult delete(Integer id){
        TWorkscore tWorkscore=tWorkscoreService.getById(id);
        tWorkscore.setStatus(0);
        return toAjax(tWorkscoreService.saveOrUpdate(tWorkscore));
    }

}
